﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using GestionDeCinebox.Classes;
using MicroMvvm;
using System.Data.SqlClient;
using System.Data;
using System.Windows.Input;
using System.Windows;

namespace GestionDeCinebox.ViewModel
{
    public class CalenViewModel : ObservableObject
    {
        private ObservableCollection<Calendrier> listCal;
        private ObservableCollection<Salle> listSalle;
        private ObservableCollection<Film> listFilm;
        private ObservableCollection<TypeDeBillet> listTypeDeBillet;
        private Boolean isVisibleButton;
        private Boolean isFilmChecked;
        private Boolean isOpenChildCelen;
        private Film filmSelected = null;
        private Salle salleSelected = null;
        private Calendrier tempsSelected = null;
        private String nomFilmSelect;
        private String nomSalleSelect;
        private String valueTempsSelect;
        private int quantiteBillet;

        public CalenViewModel()
        {
            listCal = new ObservableCollection<Calendrier>();
            listFilm = new ObservableCollection<Film>();
            listSalle = new ObservableCollection<Salle>();
            listTypeDeBillet = new ObservableCollection<TypeDeBillet>();

            IsVisibleButton = false;
            quantiteBillet = 0;
        }

        public ICommand ReserverBillet
        {
            get { return new RelayCommand(DoReserverBillet); }
        }

        public ICommand CloseChildCalen
        {
            get { return new RelayCommand(DoCloseChildCalen); }
        }

        public ICommand InitAllLists
        {
            get { return new RelayCommand(LoadLists); }
        }

        public ICommand TypeBilletSelectionChanged
        {
            get { return new RelayCommand<TypeDeBillet>(DoTypeBilletSelectionChanged); }
        }

        public ICommand TempsSelectionChanged
        {
            get { return new RelayCommand<Calendrier>(DoTempsSelectionChanged); }
        }

        public ICommand SalleSelectionChanged
        {
            get { return new RelayCommand<Salle>(DoSalleSelectionChanged); }
        }

        public ICommand FilmSelectionChanged
        {
            get { return new RelayCommand<Film>(DoFilmSelectionChanged); }
        }

        public ICommand VoirCalendrier
        {
            get { return new RelayCommand(DoVoirCalendrier); }
        }

        void DoReserverBillet()
        {
            try
            {
                if (MainWindow.personne == null)
                    throw new Exception("Vous devez entrer en depart!");
                //for (int i = 0; i < QuantiteBillet; i++ )
                //{
                //    /////reserver
                //    String idB = "";
                //    Reserver(MainWindow.personne.Id, idB);

                //}
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        void DoTypeBilletSelectionChanged(TypeDeBillet t)
        {
            //calcul quantite de billet qui ne reserve jamais
            QuantiteBillet = CalculateDeBilletJamaisReserve(tempsSelected.IdCal, salleSelected.IdSalle, t.Id);
        }

        void DoCloseChildCalen()
        {
            IsOpenChildCelen = false;
        }

        void DoVoirCalendrier()
        {
            IsOpenChildCelen = true;
            LoadListTypeBillet();
        }

        void DoTempsSelectionChanged(Calendrier c)
        {
            if (c != null)
            {
                tempsSelected = c;
                ValueTempsSelect = tempsSelected.Temps;
                IsVisibleButton = true;
            }
        }

        void DoSalleSelectionChanged(Salle s)
        {
            if (s != null)
            {
                salleSelected = s;
                NomSalleSelect = s.NomDeSalle;
            }
        }

        void DoFilmSelectionChanged(Film f)
        {
            if (f != null)
            {
                SqlParameter p = new SqlParameter("@IDFilm", f.IdFilm);
                LoadListCal("dbo.spDonnerLaListDuTemps_DeIDFilm", p);

                filmSelected = f;
                NomFilmSelect = f.NomF;
                ValueTempsSelect = "";
                IsFilmChecked = true;
            }
        }

        void LoadLists()
        {
            LoadListSalle("dbo.spDonnerLaListeDuSalle", null);
            LoadListFilm("dbo.sp_TatCaPhim", null);
        }

        int CalculateDeBilletJamaisReserve(String idCal, String idSal, String idType)
        {
            int quantite = 0;
            using (SqlCommand command = new SqlCommand("dbo.sp_LaySoVeChuaDat"))
            {
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = DataProvider.Con;
                command.Parameters.Add(new SqlParameter("@idCal", idCal));
                command.Parameters.Add(new SqlParameter("@idSal", idSal));
                command.Parameters.Add(new SqlParameter("@idType", idType));

                using (SqlDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        quantite = reader.GetInt32(reader.GetOrdinal("Quantite"));
                    }
                }
            }
            return quantite;
        }

        void LoadListTypeBillet()
        {
            if (ListTypeDeBillet.Count > 0)
            {
                ListTypeDeBillet.Clear();
            }
            using (SqlCommand command = new SqlCommand("dbo.spDonnerListeDuTypeBillet"))
            {
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = DataProvider.Con;

                using (SqlDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        TypeDeBillet t = new TypeDeBillet();
                        t.Id = reader.GetString(reader.GetOrdinal("IDTypeDeBillet"));
                        t.Prix = reader.GetString(reader.GetOrdinal("Prix"));
                        t.NomDeType = reader.GetString(reader.GetOrdinal("NomdeType"));

                        ListTypeDeBillet.Add(t);
                    }
                }
            }
        }

        void LoadListCal(String nameStore, SqlParameter para)
        {
            if (ListCal.Count > 0)
            {
                ListCal.Clear();
            }
            using (SqlCommand command = new SqlCommand(nameStore))
            {
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = DataProvider.Con;
                if (para != null)
                {
                    command.Parameters.Add(para);
                }

                using (SqlDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        Calendrier c = new Calendrier();
                        c.IdCal = reader.GetString(reader.GetOrdinal("IDCalendrier"));
                        c.Temps = reader.GetDateTime(reader.GetOrdinal("Temps")).ToString();
                        c.IdFilm = reader.GetString(reader.GetOrdinal("IDFilm"));
                        bool exists = false;
                        for (int i = 0; i < ListCal.Count; i++ )
                        {
                            if(c.Temps.Equals(ListCal[i].Temps))
                            {
                                exists = true;
                                break;
                            }
                        }
                        if (!exists)
                        {
                            ListCal.Add(c);
                        }
                    }
                }
            }
        }

        void LoadListSalle(String nameStore, SqlParameter para)
        {
            if (ListSalle.Count > 0)
            {
                ListSalle.Clear();
            }
            using (SqlCommand command = new SqlCommand(nameStore))
            {
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = DataProvider.Con;
                if (para != null)
                {
                    command.Parameters.Add(para);
                }

                using (SqlDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        Salle s = new Salle();
                        s.IdSalle = reader.GetString(reader.GetOrdinal("IDSalle"));
                        int n = reader.GetInt32(reader.GetOrdinal("NombreBillet"));
                        s.NombreDeBillet = n.ToString();
                        s.NomDeSalle = reader.GetString(reader.GetOrdinal("NomdeSalle"));
                        ListSalle.Add(s);
                    }
                }
            }
        }

        void LoadListFilm(String nameStore, SqlParameter para)
        {
            if (ListFilm.Count > 0)
            {
                ListFilm.Clear();
            }
            using (SqlCommand command = new SqlCommand(nameStore))
            {
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = DataProvider.Con;
                if (para != null)
                {
                    command.Parameters.Add(para);
                }

                using (SqlDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        Film f = new Film();
                        f.IdFilm = reader.GetString(reader.GetOrdinal("IDFilm"));
                        f.NomF = reader.GetString(reader.GetOrdinal("NomF"));
                        f.Author = reader.GetString(reader.GetOrdinal("Author"));
                        f.Acteurs = reader.GetString(reader.GetOrdinal("Acteurs"));
                        f.Info = reader.GetString(reader.GetOrdinal("Info"));
                        int duree = reader.GetInt32(reader.GetOrdinal("Duree"));
                        f.Duree = duree.ToString() + " minutes";
                        f.Lang = reader.GetString(reader.GetOrdinal("Language"));
                        f.TypeF = reader.GetString(reader.GetOrdinal("TypeF"));
                        f.Img = "";
                        ListFilm.Add(f);
                    }
                }
            }
        }

        void Reserver(String idMem, String idBillet)
        {
            using (SqlCommand command = new SqlCommand("dbo.sp_DatVe"))
            {
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = DataProvider.Con;
                command.Parameters.Add(new SqlParameter("@idMem", idMem));
                command.Parameters.Add(new SqlParameter("@idBillet", idBillet));

                command.ExecuteNonQuery();
            }
        }

        public ObservableCollection<Film> ListFilm
        {
            get { return listFilm; }
            set
            { 
                listFilm = value;
                RaisePropertyChanged("ListFilm");
            }
        }

        public ObservableCollection<Salle> ListSalle
        {
            get { return listSalle; }
            set 
            { 
                listSalle = value;
                RaisePropertyChanged("ListSalle");
            }
        }

        public ObservableCollection<Calendrier> ListCal
        {
            get { return listCal; }
            set
            { 
                listCal = value;
                RaisePropertyChanged("ListCal");
            }
        }

        public Boolean IsVisibleButton
        {
            get { return isVisibleButton; }
            set 
            { 
                isVisibleButton = value;
                RaisePropertyChanged("IsVisibleButton");
            }
        }



        public Boolean IsFilmChecked
        {
            get { return isFilmChecked; }
            set 
            { 
                isFilmChecked = value;
                RaisePropertyChanged("IsFilmChecked");
            }
        }

        public Boolean IsOpenChildCelen
        {
            get { return isOpenChildCelen; }
            set 
            { 
                isOpenChildCelen = value;
                RaisePropertyChanged("IsOpenChildCelen");
            }
        }

        public String ValueTempsSelect
        {
            get { return valueTempsSelect; }
            set
            {
                valueTempsSelect = value;
                RaisePropertyChanged("ValueTempsSelect");
            }
        }

        public String NomSalleSelect
        {
            get { return nomSalleSelect; }
            set
            {
                nomSalleSelect = value;
                RaisePropertyChanged("NomSalleSelect");
            }
        }

        public String NomFilmSelect
        {
            get { return nomFilmSelect; }
            set
            {
                nomFilmSelect = value;
                RaisePropertyChanged("NomFilmSelect");
            }
        }

        public int QuantiteBillet
        {
            get { return quantiteBillet; }
            set
            {
                quantiteBillet = value;
                RaisePropertyChanged("QuantiteBillet");
            }
        }

        public ObservableCollection<TypeDeBillet> ListTypeDeBillet
        {
            get { return listTypeDeBillet; }
            set
            {
                listTypeDeBillet = value;
                RaisePropertyChanged("ListTypeDeBillet");
            }
        }
    }
}
